import { ys } from "ys-ts";
import { nameApiAxios } from "../plugins/axios/init";
import { IResponseData } from "ys-ts/dist/types";
import { IBaseDescriptionRes, IBaseRes } from "./base";

interface IColumn extends IBaseRes, IBaseDescriptionRes {
  label: string;
  valueType: string;
  format: string;

  unique: boolean;
  primaryKey: boolean;
  notNull: boolean;
  index: boolean;
}

interface ITable extends IBaseRes, IBaseDescriptionRes {
  label: string;
  isBase: boolean;
  baseTables: string[];
  columns: IColumn[];
}

interface IColumnBaseReq {
  tableName: string;
}

interface ICreateColumnReq extends IColumnBaseReq {
  column: IColumn;
}

interface IDeleteColumnReq extends IColumnBaseReq {
  columnName: string;
}

export function useApiTable() {
  const apiUtil = ys.getApiUtil(
    "department",
    ys.getAxios(nameApiAxios),
    "/table"
  );

  function list() {
    return apiUtil.getWithApiConfig<IResponseData<Record<string, ITable>>>(
      "",
      {}
    );
  }

  function createColumn(data: ICreateColumnReq) {
    return apiUtil.postWithApiConfig("/column", {
      data,
    });
  }

  function deleteColumn(params: IDeleteColumnReq) {
    return apiUtil.postWithApiConfig("/column", {
      params,
    });
  }

  return {
    list,
    createColumn,
    deleteColumn,
  };
}
